package com.dfbz.dao;

import com.dfbz.entity.Article;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;
import java.util.Map;

public class ArticleDao extends baseDao {
    public Integer countByTitle(String title) {
        try {
            return jdbcTemplate.queryForObject("SELECT count(1) from article where title like?",Integer.class,"%"+title+"%");
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<Article> currPageData(String title, Integer startIndex, Integer pageSize) {

        List<Article> articleList = jdbcTemplate.query(
                "select * from article where title like ? order by publish_date desc limit ?,? ",
                new BeanPropertyRowMapper<>(Article.class),
                "%" + title + "%", startIndex, pageSize);

        return articleList;
    }

    /**
     * 添加文章
     * @param article
     */
    public void add(Article article) {
        jdbcTemplate.update("insert into article values(null,?,?,?,?,?,?)",
                article.getTitle(),
                article.getContent(),
                article.getBrowseCount(),
                article.getPublishDate(),
                article.getPublishRealName(),
                article.getUserId()
        );

    }

    /**
     * 更新
     * @param id
     */
    public void updateBrowserCount(Long id) {
        jdbcTemplate.update("UPDATE article SET browse_count=browse_count+1 where id=? ",id);
    }

    /**
     * 文章详情
     * @param id
     * @return
     */
    public Article findById(Long id) {
        return jdbcTemplate.queryForObject("select * from article where id=?",new BeanPropertyRowMapper<>(Article.class),id);
    }

    /**
     * 收藏次数
     * @param
     * @return
     */

    public Integer countFavoriteByArticleId(Long ArticleId) {
        return jdbcTemplate.queryForObject("SELECT count(1) FROM favorite  where a_id=?",Integer.class,ArticleId);
    }

    /**
     * 我是否收藏过这篇文章
     * @param loginUserId
     * @param articlrId
     * @return
     */
    public Integer countByUserIdAndArticleId(Long loginUserId, Long articlrId) {
        try {
            return jdbcTemplate.queryForObject("select count(1) from favorite where u_id=? and a_id=?", Integer.class, loginUserId, articlrId);

        } catch (EmptyResultDataAccessException exception) {
            // 如果是没有查询到则返回null
            return null;
        }
    }

    /**
     *我关注的人 是否页收藏过我关注的文章
     * @param loginUserId
     * @param articleId
     * @return
     */

    public List<Map<String, Object>> findUserFavorite(Long loginUserId, Long articleId) {
        String sql="        select u.id userId,u.real_name realName from userfocus uf\n" +
                "        left join user u on u.id=uf.user_focus_id\n" +
                "        left join favorite f on f.u_id=u.id\n" +
                "        where uf.user_id=? and f.a_id=?";
        return jdbcTemplate.queryForList(sql, loginUserId, articleId);
    }

    /**
     * 取消关注
     * @param loginUserId
     * @param articleID
     */
    public void unFoucs(Long loginUserId, Long articleID) {
        jdbcTemplate.update("DELETE from favorite where u_id=? and a_id=?",loginUserId,articleID);
    }

    /**
     * 加关注
     * @param loginUserId
     * @param articleID
     */
    public void Foucs(Long loginUserId, Long articleID) {
        jdbcTemplate.update("INSERT INTO favorite VALUES(?,?)",loginUserId,articleID);
    }

    public List<Article> favoritePage(Long id, String title, Integer startIndex, Integer pageSize) {
        List<Article> articleList = jdbcTemplate.query(
                "select a.* from favorite f left join article a on f.a_id=a.id where f.u_id=? and title like ?  order by publish_date desc limit ?,?",
                new BeanPropertyRowMapper<>(Article.class),
                id, "%" + title + "%", startIndex, pageSize);

        return articleList;
    }

    public Integer countByFavorite(Long id, String title) {
        try {
            return jdbcTemplate.queryForObject(
                    "select count(1) from favorite f left join article a on f.a_id=a.id where f.u_id=? and title like ?",
                    Integer.class, id, "%" + title + "%");

        } catch (EmptyResultDataAccessException exception) {
            // 如果是没有查询到则返回null
            return null;
        }
    }
}
